<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="800"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-row :gutter="10">
        <a-col :span="24">
          <a-form-item label="商品分类" field="category_id">
            <a-tree-select
              v-model="formData.category_id"
              :data="optionData.category_id"
              :field-names="{ key: 'id', title: 'category_name', icon: 'customIcon' }"
              placeholder="请选择商品分类"
              allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品类型" field="goods_type">
            <sa-radio v-model="formData.goods_type" dict="app_goods_type" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品名称" field="goods_name">
            <a-input v-model="formData.goods_name" placeholder="请输入商品名称" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品封面" field="cover_image">
            <sa-upload-image v-model="formData.cover_image" :limit="3" :multiple="false" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品轮播" field="banner">
            <sa-upload-image v-model="formData.banner" :limit="5" :multiple="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品描述" field="describe">
            <a-textarea v-model="formData.describe" placeholder="请输入商品描述" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="商品价格" field="goods_price">
            <a-input-number v-model="formData.goods_price" :min="0.01" :max="10000" :precision="2" :step="1" placeholder="请输入商品价格" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="划线价格" field="line_price">
            <a-input-number v-model="formData.line_price" :min="0.01" :max="10000" :precision="2" :step="1" placeholder="请输入划线价格" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="库存总量" field="stock_total">
            <a-input-number v-model="formData.stock_total" :min="1" :max="10000" :step="1" placeholder="请输入库存总量" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品详情" field="content">
            <ma-wangEditor v-model="formData.content" :height="400" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="初始销量" field="sales_initial">
            <a-input-number v-model="formData.sales_initial" :min="0" :max="10000" :step="1" placeholder="请输入初始销量" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="实际销量" field="sales_actual">
            <a-input-number v-model="formData.sales_actual" :min="0" :max="10000" :step="1" placeholder="请输入实际销量" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品状态" field="status">
            <sa-radio v-model="formData.status" dict="data_status" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="排序" field="sort">
            <a-input-number v-model="formData.sort" :min="1" :max="100" :step="1" placeholder="请输入排序" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import api from '../../api/goods/goods'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const optionData = reactive({
  category_id: [],
})

let title = computed(() => {
  return '商品列表' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  id: null,
  category_id: null,
  goods_type: 1,
  goods_name: '',
  cover_image: '',
  banner: '',
  describe: '',
  goods_price: '',
  line_price: '',
  stock_total: 1000,
  content: '',
  sales_initial: 0,
  sales_actual: 0,
  status: 1,
  sort: 50,
})

// 验证规则
const rules = {
  category_id: [{ required: true, message: '商品分类必需填写' }],
  goods_type: [{ required: true, message: '商品类型必需填写' }],
  goods_name: [{ required: true, message: '商品名称必需填写' }],
  cover_image: [{ required: true, message: '商品封面必需填写' }],
  describe: [{ required: true, message: '商品描述必需填写' }],
  goods_price: [{ required: true, message: '商品价格必需填写' }],
  stock_total: [{ required: true, message: '库存总量必需填写' }],
  content: [{ required: true, message: '商品详情必需填写' }],
  sales_initial: [{ required: true, message: '初始销量必需填写' }],
  sales_actual: [{ required: true, message: '实际销量必需填写' }],
  status: [{ required: true, message: '商品状态必需填写' }],
  sort: [{ required: true, message: '排序必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {
  const resp_category_id = await commonApi.commonGet('/app/saiuser/goods/AppGoodsCategory/index')
  optionData.category_id = resp_category_id.data
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      if (key == 'goods_price' || key == 'line_price') {
        formData[key] = Number.parseFloat(data[key])
      } else {
        formData[key] = data[key]
      }
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
